(function($){
	var Popeye = function(options){
		this.container = options.wrap;
		this.images = options.imgs;
		this.imgbox = options.imge;
		this.navi = options.navi;
		this.prev = options.prev;
		this.enlarge = options.enla;
		this.compass = options.comp;
		this.next = options.next;
		this.wait = options.wait;
		this.contents = options.cons;

		this.init();
	};
	
	Popeye.prototype = {
		init: function(){
		
			this.lock = true;
			this.current = 0;
			this.length = this.contents.length;
		
			var array = [this.prev, this.enlarge, this.compass, this.next],
				that = this;
				
			for(var i=0; i<array.length; i++){
				$.helpe.addEvent(array[i], 'mouseover', function(e){
					this.style.backgroundColor = '#A1C436';
				});
				$.helpe.addEvent(array[i], 'mouseout', function(e){
					this.style.backgroundColor = 'transparent';
				});
			}
			
			$.helpe.addEvent(this.container, 'mousemove', function(e){
				that.effectClear(that.delayHandler);
				if(that.lock){
					that.lock = false;
					that.effectClear(that.navi.effectHandler);
					that.effectClear(that.contents[that.current].effectHandler);
					that.transparent(that.navi, 1);
					that.transparent(that.contents[that.current], 1, 100);
				}
				e.preventDefault();
				e.stopPropagation();
			});
			
			$.helpe.addEvent(document, 'mousemove', function(e){
				if(!that.lock){
					that.effectClear(that.delayHandler);
					that.delayHandler = window.setTimeout(function(){
						that.lock = true;
						that.transparent(that.navi, -1);
						that.transparent(that.contents[that.current], -1, 100);
					}, 50);
				}
			});
			
			$.helpe.addEvent(this.next, 'click', function(e){
				that.images[that.current].style.display = 'none';
				that.contents[that.current].style.display = 'none';
				that.current++;
				if(that.current >= that.length)	that.current = 0;
				that.images[that.current].style.display = '';
				that.contents[that.current].style.display = '';
			});
			$.helpe.addEvent(this.prev, 'click', function(e){
				that.images[that.current].style.display = 'none';
				that.contents[that.current].style.display = 'none';
				that.current--;
				if(that.current < 0) that.current = that.length - 1;
				that.images[that.current].style.display = '';
				that.contents[that.current].style.display = '';
			});
			$.helpe.addEvent(this.enlarge, 'click', function(e){
				that.expand(that.imgbox, 1);
			});
			$.helpe.addEvent(this.compass, 'click', function(e){
				console.log('compass');
			});
		},
		
		expand: function(elem, direc){
			if(direc < 0){
				array = [{'begin':500,'change':-260,'name':'width'},
						 {'begin':373,'change':-200,'name':'height'}];
				this.effect2(elem, array);
			}else{
				array = [{'begin':240,'change':260,'name':'width'},
						 {'begin':173,'change':200,'name':'height'}];
				this.effect2(elem, array);
			}
		},
		
		
		
		spring: function(elem, direc, numb){
			var maxNumb = numb || 60;
			if(direc < 0){
				elem.style.height = '60px';
				elem.style.display = '';
				this.effect(elem, maxNumb, -maxNumb, 'height');
			}else{
				elem.style.height = '0px';
				elem.style.display = '';	
				this.effect(elem, 0, maxNumb, 'height');
			}
		},
		
		transparent: function(elem, direc, numb){
			var maxNumb = numb || 60;
			if(direc < 0){
				elem.style.opacity = maxNumb;
				elem.style.display = '';
				elem.style.filter = 'alpha(opacity=' + maxNumb + ')';
				this.effect(elem, maxNumb, -maxNumb, 'opacity');
			}else{
				elem.style.opacity = 0;
				elem.style.display = '';
				elem.style.filter = 'alpha(opacity=0)';
				this.effect(elem, 0, maxNumb, 'opacity');
			}
		},
		
		effect: function(elem, begin, change, property){
			var timer = 0,
				that = this,
				duration = 12,
				effectFun = this.easing['swing'],
				varial;
			
			this.effectClear(elem.effectHandler);
			elem.effectHandler = window.setInterval(function(){
				if(timer ++ >= duration){
					if('opacity' == property){
						elem.style.opacity = (begin + change)/100;
						elem.style.filter = 'alpha(opacity=' + (begin + change) + ')';
					}else{
						elem.style[property] = begin + change + 'px';
					}
					if(begin+change == 0) elem.style.display = 'none';
					window.clearInterval(elem.effectHandler);
					elem.effectHandler = null;
				}else{
					varial = effectFun(timer, begin, change, duration);
					if('opacity' == property){
						elem.style.opacity = (varial / 100);
						elem.style.filter = 'alpha(opacity=' + varial + ')';
					}else{
						elem.style[property] = varial + 'px';
					}
				}
			}, 12);
		},
		/**
		 * @param array {Array} Like this [
		 * {
		 *  'begin': beginValue
		 *  'change': changeValue
		 *  'name': nameValue
		 * } ... ]
		 */
		effect2: function(elem, array){
			var timer = 0,
				that = this,
				duration = 12,
				effectFun = this.easing['swing'],
				varial, property, begin, change;
				
			this.effectClear(elem.effectHandler);
			elem.effectHandler = window.setInterval(function(){
				if(timer ++ >= duration){
					for(var i=0; i<array.length; i++){
						begin = array[i].begin;
						change = array[i].change;
						property = array[i].name;
						if('opacity' == property){
							elem.style.opacity = (begin + change)/100;
							elem.style.filter = 'alpha(opacity=' + (begin + change) + ')';
						}else{
							elem.style[property] = begin + change + 'px';
						}
					}
					if(begin+change == 0) elem.style.display = 'none';
					window.clearInterval(elem.effectHandler);
					elem.effectHandler = null;
				}else{
					for(var i=0; i<array.length; i++){
						begin = array[i].begin;
						change = array[i].change;
						property = array[i].property;
						varial = effectFun(timer, begin, change, duration);
						if('opacity' == property){
							elem.style.opacity = (varial / 100);
							elem.style.filter = 'alpha(opacity=' + varial + ')';
						}else{
							elem.style[property] = varial + 'px';
						}	
					}
				}
			}, 12);
		},
		
		effectClear: function(handler){
			if(handler){
				window.clearInterval(handler);
				handler = null;
			}
		},
		
		easing: {
			swing : function(t, b, c, d){
				return ((-Math.cos(t / d * Math.PI) / 2) + 0.5) * c + b;
			},
			easeInExpo: function(t, b, c, d){
				return (t === 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b;
			},
			easeOutExpo: function(t, b, c, d){
				return (t === d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
			}
		}
	};
	
	window.Popeye = Popeye;
})($$L);